package de.dopichaj.labrador.inex;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import de.dopichaj.labrador.search.NEXISearcher;
import de.dopichaj.labrador.search.QueryParseException;
import de.dopichaj.labrador.search.SearchException;
import de.dopichaj.labrador.search.hit.DocHitMap;


public class Topic extends DefaultHandler {

    private int id;
    private String type;
    private String title;
    private String casTitle;
    private NEXISearcher searcher;
    
    private StringBuffer content;
    
    public Topic() {
        super();
    }
    
    public int getId() {
        return id;
    }
    
    public String getType() {
        return type;
    }
    
    public String getTitle() {
        return title;
    }
    
    public String getCASTitle() {
        return casTitle;
    }

    @Override
    public void characters(char[] ch, int start, int length) {
        content.append(new String(ch, start, length));
    }

    @Override
    public void endElement(String uri, String localName, String qName) {
        if (qName.equals("title")) {
            title = content.toString();
        } else if (qName.equals("castitle")) {
            casTitle = content.toString();
        }
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) {
        if (qName.equals("inex_topic")) {
            
            if (attributes.getValue("topic_id") != null) {
                id = Integer.parseInt(attributes.getValue("topic_id"));
                type = attributes.getValue("query_type");
                if (type == null) {
                    type = "CO+S";
                }
            }
        }
        content = new StringBuffer();
    }
    
    public void setSearcher(NEXISearcher searcher) {
        this.searcher = searcher;
    }
    
    /**
     * Search for this topic's CO title.
     * @return
     *      The search results, or <code>null</code> if the CO title is empty.
     * @throws SearchException
     * @throws QueryParseException
     */
    public DocHitMap searchCO() throws SearchException, QueryParseException {

        if (getTitle() != null && !getTitle().equals("")) {
            return searcher.nexiSearch(getTitle());
        } else {
            return null;
        }
    }
    
    /**
     * Search for this topic's CAS title.
     * @return
     *      The search results, or <code>null</code> if the CAS title is empty.
     * @throws SearchException
     * @throws QueryParseException
     */
    public DocHitMap searchCAS() throws SearchException, QueryParseException {

        if (getCASTitle() != null && !getCASTitle().equals("")) {
                return searcher.nexiSearch(getCASTitle());
        } else {
            return null;
        }
    }
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/